package com.lpf.flyweight;

import java.util.HashMap;
import java.util.Map;

// 享元工厂类 - 字符工厂
class CharacterFactory {
    private Map<Character, ConcreteCharacter> characters = new HashMap<>();

    public MyCharacter getCharacter(char symbol) {
        // 如果字符已经存在，则返回共享的字符对象
        if (characters.containsKey(symbol)) {
            return characters.get(symbol);
        } else {
            // 否则创建一个新的字符对象并存储
            ConcreteCharacter character = new ConcreteCharacter(symbol);
            characters.put(symbol, character);
            return character;
        }
    }
}
